
#import "USAViewController.h"
#import "WXNetworkService.h"
#import "MovieModel.h"
#import "USACell.h"
#import "PosterView.h"
@interface USAViewController ()

- (void)initListView;

- (void)initPosterView;

- (void)initNavigationBar;

- (void)requestData;

- (void)refreshUI;

@end

@implementation USAViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title=@"北美榜";
    }
    return self;
}

- (void)loadView
{
    [super loadView];
    [self initPosterView];
    [self initListView];
    
    [self initNavigationBar];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	[self requestData];
}

- (void)initListView
{
    _listView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kDeviceWidth, kDeviceHeight-49-64) style:UITableViewStylePlain];
    _listView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg_main"]];
    [self.view addSubview:_listView];
    _listView.rowHeight = 101;
    _listView.delegate = self;
    _listView.dataSource = self;
}

- (void)initPosterView
{
    _posterView = [[PosterView alloc] initWithFrame:CGRectMake(0, 0, kDeviceWidth, kDeviceHeight-49)];
    _posterView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg_main"]];
    [self.view addSubview:_posterView];
}

- (void)initNavigationBar
{
    UIImageView *baseItem = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 50, 24)];
    baseItem.image = [UIImage imageNamed:@"exchange_bg_home"];
    baseItem.userInteractionEnabled = YES;
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
                                   initWithTarget:self action:@selector(exchangeSubviews:)];
    [baseItem addGestureRecognizer:tap];
    
    UIImageView *listItem = [[UIImageView alloc] initWithFrame:CGRectMake(13, 5, 24, 14)];
    listItem.image = [UIImage imageNamed:@"list_home"];
    listItem.tag = 101;
    [baseItem addSubview:listItem];
    
    UIImageView *posterItem = [[UIImageView alloc] initWithFrame:CGRectMake(13, 5, 24, 14)];
    posterItem.image = [UIImage imageNamed:@"poster_home"];
    posterItem.tag = 102;
    [baseItem addSubview:posterItem];
    posterItem.hidden = YES;
    
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:baseItem];
    self.navigationItem.rightBarButtonItem = item;
}

- (void)requestData
{
    NSArray *data = [WXNetworkService northUSAData];
    _arrayList = [[NSMutableArray alloc] initWithCapacity:data.count];
    for (NSDictionary *dic in data) {
        MovieModel *movieModel = [[MovieModel alloc] initWithContent:dic[@"subject"]];
        [_arrayList addObject:movieModel];
    }
    [self refreshUI];
}

- (void)refreshUI
{
    [_listView reloadData];
    
    [_posterView reloadPosterData:_arrayList];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _arrayList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellID = @"cell";
    USACell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil) {
        cell = [[USACell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    cell.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg_main"]];
//    cell.textLabel.text = [_arrayList[indexPath.row] title];
    cell.movieModel = _arrayList[indexPath.row];
    return  cell;
}

- (void)exchangeSubviews:(UITapGestureRecognizer *)tap
{
    UIView *baseView = [tap view];
    UIView *listerItem = [baseView viewWithTag:101];
    UIView *posterItem = [baseView viewWithTag:102];
    
    [self animationBaseView:self.view Flag:posterItem.hidden];
    //[self animationBaseView:baseView Flag:posterItem.hidden];
    
    if (posterItem.hidden) {
        posterItem.hidden = NO;
        listerItem.hidden = YES;
    }else{
        posterItem.hidden = YES;
        listerItem.hidden = NO;
    }
}



- (void)animationBaseView:(UIView *)baseView Flag:(BOOL)flag
{
    [UIView animateWithDuration:0.5 animations:^{
        
        [baseView exchangeSubviewAtIndex:0 withSubviewAtIndex:1];
        if (flag==0)
        {
            [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:baseView cache:YES];
        }
        else
        {
            [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:baseView cache:YES];
        }
        
    }];
}


@end
